package com.info33.platform.config.pay;

import com.alipay.easysdk.kernel.Config;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * @author debj
 * @version 1.0
 * @date 2021/2/26 17:24
 */
@Component
public class AliPayConfig {

    private AliPayConfig() {
    }

    private static String getPrivateKey(String appId) {
        InputStream stream = AliPayConfig.class.getResourceAsStream("/fixture/zfb/privateKey.json");
        Map<String, String> result = new Gson().fromJson(new InputStreamReader(stream), new TypeToken<Map<String, String>>() {
        }.getType());
        return result.get(appId);
    }

    public static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        //线上部署开启一下网关
        config.gatewayHost = "openapi.alipay.com";
        config.appId = "2021003129649432";
        config.signType = "RSA2";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = getPrivateKey(config.appId);

        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/fixture/zfb/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/fixture/zfb/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/fixture/zfb/alipayRootCert.crt -->";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2g8tB4HvUHxr0+kHsYKaU3v1CCpqx" +
                "66l6XyR+zGEaX0vfW1v9hLhEZJkHWavywxhNiJtTilHA/WbcHx++smYCDaRytnXomJRyYB920unIugHZwvEOqRVg6U" +
                "0daOGW1MlM6JDkk/0iAFhgymhAibnSl9AuXGstbM137hZ8oaznd6Ifsdru16jZo/iP2KTJulVzfL/rmFwbMVVC5KFf" +
                "6xCxmyQSZI3ilrHQ8XxRFrdCoRTYmuFiBNeqNWSJaVsSvMQgPqCOknkf0cEVZyviP65lmOIn+f9XCNw79pF7EGOc5J" +
                "kbucuZ2jbLZoHDJwT7S9HOFEzhWE3YwZrgE5/yUFY4QIDAQAB";

        //可设置异步通知接收服务地址（可选）
//        config.notifyUrl = "";
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        config.encryptKey = "H8DQiUOMykdEy9C9wqsm+g==";

        return config;
    }

}
